<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="配置名称" prop="configName">
        <el-input
          v-model="queryParams.configName"
          placeholder="请输入配置名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="配置类型" prop="configType">
        <el-select v-model="queryParams.configType" placeholder="请选择配置类型">
          <el-option label="LLM大语言模型" value="llm"></el-option>
          <el-option label="STT语音识别" value="stt"></el-option>
          <el-option label="TTS语音合成" value="tts"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <!--      <el-col :span="1.5">-->
      <!--        <el-button-->
      <!--          type="primary"-->
      <!--          plain-->
      <!--          icon="el-icon-plus"-->
      <!--          size="mini"-->
      <!--          @click="handleAdd"-->
      <!--          v-hasPermi="['business:LllmConfig:add']"-->
      <!--        >新增-->
      <!--        </el-button>-->
      <!--      </el-col>-->
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['business:LllmConfig:edit']"
        >修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['business:LllmConfig:remove']"
        >删除
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="LllmConfigList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="配置ID，主键" align="center" prop="configId"/>
      <!--      <el-table-column label="创建用户ID" align="center" prop="userId"/>-->
      <el-table-column label="配置名称" align="center" prop="configName"/>
      <el-table-column label="配置类型" align="center" prop="configType">
        <template slot-scope="scope">
          <span v-if="scope.row.configType === 'llm'">LLM大语言模型</span>
          <span v-else-if="scope.row.configType === 'stt'">STT语音识别</span>
          <span v-else-if="scope.row.configType === 'tts'">TTS语音合成</span>
          <span v-else>{{ scope.row.configType }}</span>
        </template>
      </el-table-column>
      <el-table-column label="LLM模型类型" align="center" prop="modelType"/>
      <el-table-column label="服务提供商" align="center" prop="provider">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.model_provider" :value="scope.row.provider"/>
        </template>
      </el-table-column>

      <!--      <el-table-column label="配置描述" align="center" prop="configDesc"/>-->
      <!--      <el-table-column label="APP ID" align="center" prop="appId"/>-->
      <!--      <el-table-column label="API密钥" align="center" prop="apiKey"/>-->
      <!--      <el-table-column label="API密钥" align="center" prop="apiSecret"/>-->
      <!--      <el-table-column label="Access Key" align="center" prop="ak"/>-->
      <!--      <el-table-column label="Secret Key" align="center" prop="sk"/>-->
      <!--      <el-table-column label="API地址" align="center" prop="apiUrl"/>-->
      <!--      <el-table-column label="是否为默认配置: 1-是, 0-否" align="center" prop="isDefault"/>-->
      <!--      <el-table-column label="状态" align="center" prop="state">-->
      <!--        <template slot-scope="scope">-->
      <!--          <dict-tag :options="dict.type.enable_status" :value="scope.row.state"/>-->
      <!--        </template>-->
      <!--      </el-table-column>-->
      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="更新时间" align="center" prop="updateTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.updateTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['business:LllmConfig:edit']"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['business:LllmConfig:remove']"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改模型配置(模型、语音识别、语音合成等)对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="1200px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="150px">
        <!--        <el-form-item label="创建用户ID" prop="userId">-->
        <!--          <el-input v-model="form.userId" placeholder="请输入创建用户ID"/>-->
        <!--        </el-form-item>-->
        <el-form-item label="配置类型" prop="configType">
          <el-select v-model="form.configType" disabled placeholder="请选择配置类型">
            <el-option label="LLM大语言模型" value="llm"></el-option>
            <el-option label="STT语音识别" value="stt"></el-option>
            <el-option label="TTS语音合成" value="tts"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="模型类型" prop="modelType">
          <el-input v-model="form.modelType" disabled placeholder="请选择模型类型"/>
        </el-form-item>
        <el-form-item label="服务提供商" prop="provider">
          <el-select v-model="form.provider" disabled placeholder="请选择服务提供商">
            <el-option
              v-for="dict in dict.type.model_provider"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="配置名称" prop="configName">
          <el-input v-model="form.configName" placeholder="请输入配置名称"/>
        </el-form-item>
        <el-form-item label="配置描述" prop="configDesc">
          <el-input v-model="form.configDesc" type="textarea" placeholder="请输入内容"/>
        </el-form-item>
        <!--        <el-form-item label="APP ID" prop="appId">-->
        <!--          <el-input v-model="form.appId" placeholder="请输入APP ID"/>-->
        <!--        </el-form-item>-->
        <!--        <el-form-item label="API密钥" v-if="form.configType =='llm'" prop="apiKey">-->
        <!--          <el-input v-model="form.apiKey" placeholder="请输入API密钥"/>-->
        <!--        </el-form-item>-->
        <!--        <el-form-item label="API地址" v-if="form.configType =='llm'" prop="apiUrl">-->
        <!--          <el-input v-model="form.apiUrl" placeholder="请输入API地址"/>-->
        <!--        </el-form-item>-->
        <!--        <el-form-item label="API密钥" prop="apiSecret">-->
        <!--          <el-input v-model="form.apiSecret" placeholder="请输入API密钥"/>-->
        <!--        </el-form-item>-->
        <!--        <el-form-item v-if="form.configType =='tts'" label="accessKeyId" prop="ak">-->
        <!--          <el-input v-model="form.ak" placeholder="请输入accessKeyId"/>-->
        <!--        </el-form-item>-->
        <!--        <el-form-item v-if="form.configType =='tts'" label="accessKeySecret" prop="sk">-->
        <!--          <el-input v-model="form.sk" placeholder="请输入accessKeySecret"/>-->
        <!--        </el-form-item>-->

        <!--        <el-form-item label="状态" prop="state">-->
        <!--          <el-select v-model="form.state" disabled placeholder="请选择服务提供商">-->
        <!--            <el-option-->
        <!--              v-for="dict in dict.type.enable_status"-->
        <!--              :key="dict.value"-->
        <!--              :label="dict.label"-->
        <!--              :value="dict.value"-->
        <!--            ></el-option>-->
        <!--          </el-select>-->

        <!--        </el-form-item>-->

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {listLllmConfig, getLllmConfig, delLllmConfig, addLllmConfig, updateLllmConfig} from "@/api/business/LllmConfig";

export default {
  name: "LllmConfig",
  dicts: ['model_provider', 'model_type', 'enable_status'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 模型配置(模型、语音识别、语音合成等)表格数据
      LllmConfigList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        userId: null,
        configType: null,
        modelType: null,
        provider: null,
        configName: null,
        configDesc: null,
        appId: null,
        apiKey: null,
        apiSecret: null,
        ak: null,
        sk: null,
        apiUrl: null,
        isDefault: null,
        state: null,
        createTime: null,
        updateTime: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        // userId: [
        //   {required: true, message: "创建用户ID不能为空", trigger: "blur"}
        // ],
        // configType: [
        //   {required: true, message: "配置类型(llm, stt, tts等)不能为空", trigger: "change"}
        // ],
        // provider: [
        //   {required: true, message: "服务提供商(openai, vosk, aliyun, tencent等)不能为空", trigger: "blur"}
        // ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询模型配置(模型、语音识别、语音合成等)列表 */
    getList() {
      this.loading = true;
      listLllmConfig(this.queryParams).then(response => {
        this.LllmConfigList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        configId: null,
        userId: null,
        configType: null,
        modelType: null,
        provider: null,
        configName: null,
        configDesc: null,
        appId: null,
        apiKey: null,
        apiSecret: null,
        ak: null,
        sk: null,
        apiUrl: null,
        isDefault: null,
        state: null,
        createTime: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.configId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加模型配置";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const configId = row.configId || this.ids
      getLllmConfig(configId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改模型配置";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.configId != null) {
            updateLllmConfig(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addLllmConfig(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const configIds = row.configId || this.ids;
      this.$modal.confirm('是否确认删除模型配置(模型、语音识别、语音合成等)编号为"' + configIds + '"的数据项？').then(function () {
        return delLllmConfig(configIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('business/LllmConfig/export', {
        ...this.queryParams
      }, `LllmConfig_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
